package xGRUx.MCdirGlztab;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import xGRUx.MCdirGlztab.R;
import xGRUx.MCdirGlztab.Views.StoryItem;
import xGRUx.MCdirGlztab.Views.StoryViewAdapter;
import xGRUx.MCdirGlztab.Xml.XPathHelper;
import android.app.ListActivity;
import android.app.TabActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.TabHost;

public class StoryAct extends ListActivity
{
    private StoryViewAdapter m_adapter;
    //private Runnable viewOrders;
	private List<StoryItem> m_Items;
	
	public StoryAct() {
		this.m_Items = new ArrayList<StoryItem>();
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
        setContentView(R.layout.stories);
        
        try
		{
        	XPathHelper xhlp = new XPathHelper(getAssets().open("samples.xml"));
    		List<StoryItem> tmp = xhlp.getStoryItems("root/message");
    		if(tmp != null) {
    			this.m_Items = tmp;
    		}
		} catch (IOException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        
	    this.m_adapter = new StoryViewAdapter(this, R.layout.storyitemview, this.m_Items);
	    this.setListAdapter(this.m_adapter);

	}
	
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		// Get the item that was clicked
		StoryItem item = (StoryItem)this.getListAdapter().getItem(position);
		
		if(item != null) {
			String c = item.getContents();
			getParent().getIntent().putExtra("StoryContent", c);
			
			TabActivity tab = (TabActivity)getParent();
			TabHost tabHost = tab.getTabHost();
			tabHost.setCurrentTab(1);
		}
		
	}
}
